package cn.kjcoder.redis.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author hkj
 * @email 1349091387@qq.com
 * @date 2022/1/21
 * 源码允许修改，并且本人十分乐意有 大佬 来对代码进行 随 意 调 教
 **/
@RestController
@Slf4j
@RequestMapping("/random")
public class RandomController {

    @Autowired
    private RedisTemplate redisTemplate;
    public static final String CROWDS_KEY = "change:crowds";

    @GetMapping("/crowd")
    public List<String> crowd(){
        List<String> list = null;
        try {
            //随机取出10条数据
            list = this.redisTemplate.opsForSet().randomMembers(CROWDS_KEY, 3);
            log.info("查询的结果{}",list);
        } catch (Exception e) {
            //出现异常，可能是redis连接超时或者是瘫痪了
            log.error("exception:" + e);
            //TODO 去数据库中查询
        }
        return list;
    }
}
